package design.simdi.code.final_paper.site.pages;

import java.util.Locale;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.PageParameters;
import org.apache.wicket.RedirectToUrlException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;

import design.simdi.code.final_paper.site.application.MyAuthenticatedWebSession;
import design.simdi.code.final_paper.site.panels.FeedBackPanels;
import design.simdi.code.final_paper.site.panels.NewUserWizardPanels;
import design.simdi.code.final_paper.site.panels.menu.MenuPanel;

/**
 * 
 * @author Marius Dumitriu - (May 23, 2010 - 2:41:29 AM)
 * 
 */
public abstract class BasePage extends WebPage {

	public static final String ERRORS_PANEL = "errors";

	private FeedBackPanels erori;

	public BasePage(PageParameters parameters) {
		super(parameters);
		pageInit();

	}

	public BasePage() {
		super();
		pageInit();
	}

	protected void pageInit() {
		setOutputMarkupId(true);

		add(erori = new FeedBackPanels(ERRORS_PANEL));
		erori.setOutputMarkupPlaceholderTag(true);
		erori.setOutputMarkupId(true);

		add(createHomeLink("link.home"));
		add(createLogoutLink("link.logout"));
		add(createLoginLink("link.login"));
		add(createRegisterLink("link.register"));

		add(new MenuPanel("menu"));
		add(createRoLink("ro"));
		add(createEnLink("en"));

		WebMarkupContainer container = new WebMarkupContainer("container.header.hi.user");
		container.setOutputMarkupPlaceholderTag(true);

		String user = ((MyAuthenticatedWebSession) this.getSession()).getCurrentUser() == null ? ""
				: ((MyAuthenticatedWebSession) this.getSession()).getCurrentUser().toString();

		container.add(new Label("user", user));
		container.setVisible(((MyAuthenticatedWebSession) this.getSession()).isSignedIn());
		add(container);
	}

	private Component createHomeLink(String string) {
		AjaxFallbackLink<String> link = new AjaxFallbackLink<String>(string) {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				throw new RedirectToUrlException("/home");
			}
		};
		return link;
	}

	private AjaxFallbackLink<String> createEnLink(String string) {
		AjaxFallbackLink<String> link = new AjaxFallbackLink<String>(string) {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				getSession().setLocale(new Locale("En"));
				target.addComponent(this.getPage());
			}
		};
		return link;
	}

	private AjaxFallbackLink<String> createRoLink(String string) {
		AjaxFallbackLink<String> link = new AjaxFallbackLink<String>(string) {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				getSession().setLocale(new Locale("Ro"));
				target.addComponent(this.getPage());
			}
		};
		return link;
	}

	private AjaxFallbackLink<String> createLogoutLink(String string) {
		AjaxFallbackLink<String> link = new AjaxFallbackLink<String>(string) {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				getSession().invalidate();
				target.addComponent(this.getPage());
			}
		};

		link.add(new Label("link.logout.text", getString("link.logout.text")));
		link.setOutputMarkupPlaceholderTag(true);
		link.setVisible(((MyAuthenticatedWebSession) this.getSession()).isSignedIn());
		return link;
	}

	private AjaxFallbackLink<String> createLoginLink(String string) {
		AjaxFallbackLink<String> link = new AjaxFallbackLink<String>(string) {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				throw new RedirectToUrlException("/login");
			}
		};
		link.add(new Label("link.login.text", getString("link.login.text")));
		link.setOutputMarkupPlaceholderTag(true);
		link.setVisible(!((MyAuthenticatedWebSession) this.getSession()).isSignedIn());
		return link;
	}

	private AjaxFallbackLink<String> createRegisterLink(String string) {
		AjaxFallbackLink<String> link = new AjaxFallbackLink<String>(string) {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				Page p = this.getPage();
				NewUserWizardPanels panel = new NewUserWizardPanels("content", p.get("content"));

				p.get("content").replaceWith(panel);

				p.get(BasePage.ERRORS_PANEL).setVisible(false);

				target.addComponent(p);

			}
		};
		link.add(new Label("link.register.text", getString("link.register.text")));
		link.setOutputMarkupPlaceholderTag(true);
		link.setVisible(!((MyAuthenticatedWebSession) this.getSession()).isSignedIn());
		return link;
	}

}
